import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../views/login'
// import Layout from '../views/layout'
import { getToken } from '../utils/token'
// import Chart from '../views/layout/chart'
// import Welcome from '../views/layout/welcome'
// import User from '../views/layout/user'
// import Enterprise from '../views/layout/enterprise'
// import Question from '../views/layout/question'
// import Subject from '../views/layout/subject'
const Login = () => import('../views/login')
const Layout = () => import('../views/layout')
const Chart = () => import('../views/layout/chart')
const Welcome = () => import('../views/layout/welcome')
const User = () => import('../views/layout/user')
const Enterprise = () => import('../views/layout/enterprise')
const Question = () => import('../views/layout/question')
const Subject = () => import('../views/layout/subject')

// 测试
import Slot from '../test/slot/parent.vue'
import Slot2 from '../test/slot2/parent.vue'
import Slot3 from '../test/slot3/parent.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录' }
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/layout',
    name: 'Layout',
    component: Layout,
    children: [
      //设置子路由
      {
        path: 'chart', //子路由的path前面不能加/
        name: 'Chart',
        component: Chart,
        meta: {
          title: '数据预览',
          path: '/layout/chart',
          icon: 'el-icon-pie-chart',
          roles: ['超级管理员', '管理员', '老师']
        } // 配置路由元信息
      },
      {
        path: 'user',
        name: 'User',
        component: User,
        meta: {
          title: '用户列表',
          path: '/layout/user',
          icon: 'el-icon-user',
          roles: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'enterprise',
        name: 'Enterprise',
        component: Enterprise,
        meta: {
          title: '企业列表',
          path: '/layout/enterprise',
          icon: 'el-icon-office-building',
          roles: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'question',
        name: 'Question',
        component: Question,
        meta: {
          title: '题库列表',
          path: '/layout/question',
          icon: 'el-icon-edit-outline',
          roles: ['超级管理员', '管理员', '老师', '学生']
        }
      },
      {
        path: 'subject',
        name: 'Subject',
        component: Subject,
        meta: {
          title: '学科列表',
          path: '/layout/subject',
          icon: 'el-icon-notebook-2',
          roles: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'welcome',
        name: 'Welcome',
        component: Welcome,
        meta: {
          title: '欢迎登陆',
          path: '/layout/welcome',
          icon: 'el-icon-notebook-2',
          roles: ['超级管理员', '管理员', '老师', '学生']
        }
      }
    ]
  },
  // 测试
  {
    path: '/slot',
    component: Slot
  },
  {
    path: '/slot2',
    component: Slot2
  },
  {
    path: '/slot3',
    component: Slot3
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
// 1.设置白名单(就是不需要验证是否携带token就能直接进入的页面)
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  // 2.判断要跳转的页面是不是白名单中的页面(不设置白名单会出现跳转死循环)
  if (whiteList.includes(to.path)) {
    // 是，就直接跳转
    next() //这是一个函数，使用的时候必须调用
  } else {
    // 不是，就验证是否携带token
    if (getToken()) {
      // 携带，就正常跳转
      next()
    } else {
      // 没携带，就返回登录页
      next('/login')
    }
  }
})

// 后置导航守卫(路由跳转完毕做的操作)
router.afterEach((to, from) => {
  // to:跳转到哪个页面(已跳转完毕，可以理解为当前页面)
  // from：从哪个页面跳转过来的
  // document.title：页面上方的标题
  document.title = to.meta.title || '黑马面面'
})

export default router
